package cn.tedu.boot041.controller;

import cn.tedu.boot041.entity.Student;
import cn.tedu.boot041.mapper.StudentMapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    StudentMapper mapper;

    @RequestMapping("/insert")
    public String insert(Student student) {
        mapper.insert(student);
        return "添加完成！<a href='/index.html'>点击此处返回主页面</a>，<a href='/insert.html'>点击此处继续添加学生</a>";
    }

    @RequestMapping("/delete")
    public String delete(int id) {
        mapper.deleteById(id);
        return "删除完成！<a href='/index.html'>点击此处返回主页面</a>，<a href='/select'>点击此处列表页面</a>";
    }

    @RequestMapping("/update")
    public String update(Student student) {
        mapper.update(student);
        return "修改完成！<a href='/index.html'>点击此处返回主页面</a>，<a href='/update.html'>点击此处继续修改</a>";
    }

    @RequestMapping("/select")
    public String select() {
        List<Student> list = mapper.select();

        String html = "<table border=1>";
        html += "<caption>学生列表</caption>";
        html += "<tr><th>id</th><th>姓名</th><th>年龄</th><th>性别</th><th>操作</th></tr>";
        for (Student s : list) {
            html += "<tr>";
            html += "<td>" + s.getId() + "</td>";
            html += "<td>" + s.getName() + "</td>";
            html += "<td>" + s.getAge() + "</td>";
            html += "<td>" + s.getGender() + "</td>";
            html += "<td><a href='/delete?id=" + s.getId() + "'>删除</a></td>";
            html += "</tr>";
        }
        html += "</table>";
        return html;
    }
}
